const Router = require('koa-router');
const router = new Router();
const userController = require('../controllers/userController');
const novelController = require('../controllers/novelController');
const commentController = require('../controllers/commentController');
const authMiddleware = require('../middleware/auth');

// All routes in this file are protected and require admin role
router.use(authMiddleware);
router.use(async (ctx, next) => {
  if (ctx.state.user.role !== 'admin') {
    ctx.status = 403;
    ctx.body = { error: 'Forbidden' };
    return;
  }
  await next();
});

// User management
router.get('/users', userController.getUsers);
router.post('/users', userController.createUser);
router.put('/users/:id', userController.updateUser);
router.patch('/users/:id/status', userController.updateUserStatus);
router.delete('/users/:id', userController.deleteUser);

// Novel management
router.get('/novels', novelController.getNovels);
router.post('/novels', novelController.createNovel);
router.put('/novels/:id', novelController.updateNovel);
router.patch('/novels/:id/status', novelController.updateNovelStatus);
router.delete('/novels/:id', novelController.deleteNovel);

// Comment management
router.get('/comments', commentController.getComments);
router.get('/novels/:id/comments', commentController.getCommentsByNovelId);
router.delete('/comments/:id', commentController.deleteComment);

module.exports = router;